﻿using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall.Client
{
	public class ComManager
	{
		public static void Send<T>(Socket socketClient,
			Func<CommandReq<T>> func) where T : BaseReq
		{
			var commandReq = func();
			var protocolStr = ProtocolHelper.GetProtocolStr(commandReq.Command);
			string cont = commandReq.Req.JsonSerialize();
			var sendContent = EncryptHelper.Encrypt(cont);
			var buffter = Encoding.UTF8.GetBytes($"{protocolStr}{SeparatorConfig.Transfer}{sendContent}{SeparatorConfig.Protocol}");
			try
			{
				socketClient.Send(buffter);
			}
			catch (Exception)
			{

			}

		}
		/// <summary>
		/// 输出协议信息
		/// </summary>
		/// <param name="mainCommand"></param>
		/// <param name="command"></param>
		/// <param name="respStr"></param>
		public static void ConsoleWriteResp(EnumCommand command, string respStr)
		{
			if (command != EnumCommand.Heart_HeartData)
			{
				var resp = respStr.JsonDeserialize<BaseResp>();

				var dict = ConsoleColor.Yellow.GetEnumDesxriptionDict();
				var list = dict.ToList().Select(m => (ConsoleColor)m.Key).ToList();
				list.RemoveAll(m => m == ConsoleColor.Black || m == ConsoleColor.Blue || m == ConsoleColor.DarkBlue);
				Random random = new Random();
				var color = list[random.Next(list.Count())];

				Console.WriteLine();
				Console.WriteLine($"color:{color.ToString()}");
				Console.ForegroundColor = color;
				Console.WriteLine("-------------------------------------------------------");
				Console.WriteLine($"【客户端】接收返回的数据：（时间：{DateTimeOffset.Now.ToStr()}）");
				Console.WriteLine($"	协议：{command.ToString()}");
				Console.WriteLine($"	响应消息级别：{resp.RespLevel.ToString()}");
				Console.WriteLine($"	响应字符串：{respStr}");
				Console.ResetColor();
			}
		}
	}
}
